//
//  LZQ_Network.swift
//  SwiftExercise
//
//  Created by King on 2021/6/18.
//

import UIKit
import Alamofire
import SwiftyJSON

class LZQ_Network: NSObject {
    
    struct responseData {
        var request:URLRequest?
        var response:HTTPURLResponse?
        var json:AnyObject?
        var error:NSError?
        var data:Data?
    }
    
    class func requestWith(Method method:Alamofire.HTTPMethod,URL url:String,Parameter para:[String:String]?,Token token:String?,handler:@escaping(responseData) ->Void) {
        let reachAble = LZQ_NetworkReachability.reachability.reachAble
        if reachAble {

            //默认加入token
            var params = [String:String]()
            
            guard let para = para else { return }
            
            for (key,val) in para {
                params[key] = val
            }
            params["tokenId"] = token
            
            
            let manager = Alamofire.SessionManager.default
            manager.session.configuration.timeoutIntervalForRequest = 10
            manager.request(url, method: method, parameters: params, encoding: URLEncoding.default, headers: nil).response(completionHandler: { (response) in
                let json:AnyObject! = try? JSONSerialization.jsonObject(with: response.data!, options: .allowFragments) as AnyObject
                if nil != json {
                    let res = responseData(request:response.request, response:response.response, json:json, error:response.error as NSError?, data:response.data)
                    handler(res)
                }
            })
        }
    }
    
}
